﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using DAO;
using BUS;
public partial class Admin_Pages_WRURGENTManager : BasePage
{
    public static bool Insert;
    BUS_WRURGENT busWRURGENT = new BUS_WRURGENT();
    BUS_WRLANGUAGE busWrlanguage = new BUS_WRLANGUAGE();
    BUS_WRFUNCPAY busWrfuncpay = new BUS_WRFUNCPAY();
    BUS_WRUSER busWruser = new BUS_WRUSER();
    private void Show(string Mess)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + Mess + "')", true);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            BindGrid();
            BindDrop();
        }
    }

    private void BindGrid()
    {
        grvWRURGENT.DataSource = busWRURGENT.Get_Urgent_List();
        //grvWRURGENT.DataSource = busWRURGENT._GetList().Select(p => new
        //{
        //    p.ID, p.UrgentName,
        //    UpdateDate = Common.Common.ConvertDateTime(p.UpdateDate.ToString()) == "" ? "" : Convert.ToDateTime(p.UpdateDate).ToShortDateString(),
        //    p.DayLimit,
        //    p.Active,
        //    Name = p.IDUpdateBy == (int?)null ? "" : busWruser._GetObject(q => q.ID == p.IDUpdateBy).Lastname + " " + busWruser._GetObject(q => q.ID == p.IDUpdateBy).Firstname,
        //    FuncPayName = busWrfuncpay._GetObject(Convert.ToInt32(p.IDFuncPay)).FuncPayName,
        //    LangName = p.IDLang == (int?)null ? "" : busWrlanguage._GetObject(Convert.ToInt32(p.IDLang)).LangName
                
        //}).ToList();
        grvWRURGENT.DataBind();
    }

    public void BindDrop()
    {
        dropIDFuncPay.DataSource = busWrfuncpay._GetList().Where(q => q.Active == "Y").Select(p => new { p.ID, p.FuncPayName }).ToList();
        dropIDFuncPay.DataTextField = "FuncPayName";
        dropIDFuncPay.DataValueField = "Id";
        dropIDFuncPay.DataBind();
        dropIDLang.DataSource = busWrlanguage._GetList().Where(q => q.Active == "Y").Select(p => new { p.ID, p.LangName }).ToList();
        dropIDLang.DataTextField = "LangName";
        dropIDLang.DataValueField = "Id";
        dropIDLang.DataBind();

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = true;
        pnShow.Visible = false;
        Insert = true;
        txtUrgentName.Text = "";
        txtDayLimit.Text = "";
        txtDescription.Text = "";
        txtKeywords.Text = "";


    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (grvWRURGENT.Items.Count > 0)
        {
            for (int i = 0; i < grvWRURGENT.Items.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)grvWRURGENT.Items[i].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    string Id = grvWRURGENT.Items[i].Cells[1].Text;
                    try
                    {
                        busWRURGENT._Delete(Convert.ToInt32(Id));
                    }
                    catch
                    {
                    }
                }
            }
        }
        BindGrid();
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (txtUrgentName.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập UrgentName!");
            txtUrgentName.Focus();
            return;
        }
        if (txtDayLimit.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập DayLimit!");
            txtDayLimit.Focus();
            return;
        }
        if (txtDescription.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập Description!");
            txtDescription.Focus();
            return;
        }
        if (txtKeywords.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập Keywords!");
            txtKeywords.Focus();
            return;
        }


        WRURGENT obj = new WRURGENT();
        
        obj.UrgentName = txtUrgentName.Text;
        obj.DayLimit = Convert.ToInt32(txtDayLimit.Text);
        obj.IDFuncPay = Convert.ToInt32(dropIDFuncPay.SelectedValue);
        obj.Description = txtDescription.Text;
        obj.IDLang = Convert.ToInt32(dropIDLang.SelectedValue);
        obj.Active = dropActive.SelectedValue;
        obj.Keywords = txtKeywords.Text;
        obj.UpdateDate = DateTime.Now;
        obj.IDUpdateBy = Convert.ToInt32(Session["userid"].ToString());
        if (Insert)
        {
            obj.IDCreateBy = Convert.ToInt32(Session["userid"].ToString());
            obj.CreateDate = DateTime.Now;
            busWRURGENT._Add(obj);
        }
        else
        {
            obj.ID = Convert.ToInt32(txtID.Text);
            busWRURGENT._Update(obj);
        }
        BindGrid();
        pnUpdate.Visible = false;
        pnShow.Visible = true;
        Insert = false;

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = false;
        pnShow.Visible = true;

    }

    protected void grvWRURGENT_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string Id = e.CommandArgument.ToString();
        if (e.CommandName == "Edit")
        {
            Insert = false;
            WRURGENT _WRURGENT = busWRURGENT._GetObject(Convert.ToInt32(Id));
            txtID.Text = Id;
            txtUrgentName.Text = _WRURGENT.UrgentName;
            txtDayLimit.Text = _WRURGENT.DayLimit.ToString();
            dropIDFuncPay.SelectedValue = _WRURGENT.IDFuncPay.ToString();
            txtDescription.Text = _WRURGENT.Description;
            try
            {
                dropIDLang.SelectedValue = _WRURGENT.IDLang.ToString();
            }
            catch
            {
                Show("Language is not active!");
            }
            dropActive.SelectedValue = _WRURGENT.Active.ToString();
            txtKeywords.Text = _WRURGENT.Keywords;
            pnUpdate.Visible = true;
            pnShow.Visible = false;
        }
        else if (e.CommandName == "Delete")
        {
            busWRURGENT._Delete(Convert.ToInt32(Id));
            BindGrid();
        }

    }

    protected void grvWRURGENT_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grvWRURGENT.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }
}
